package com.web.controller; /**
 * @author web
 * @version 1.0
 * @date 2023/11/8 12:50
 */



import com.web.utils.CheckCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
@Slf4j
@RestController
@RequestMapping("/checkCode")
public class CheckCodeController {
    @GetMapping
    public void checkCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session;
        // 如过有session对象就销毁，保证每次注册时的session都不一样（注册后，session开始没被销毁，所以还保留username，password， 到切换图片时，就会把session对象销毁，创建一个新的session，所以点击刷新时就没有username和password）【顺序：注册 -> registerServlet 给session添加属性 -> register.jsp显示username和password -> checkCodeServlet 销毁以前session并创建新的session（无属性） -> 注册 -> ……】
        if ((session = request.getSession(false)) != null) {
            session.invalidate();
        }
        session = request.getSession();


        OutputStream fos = response.getOutputStream();
        String checkCode = CheckCodeUtil.outputVerifyImage(100, 50, fos, 4);

        System.out.println(checkCode);
        session.setAttribute("checkCodeGen", checkCode);
    }
}
